package com.right.health.app.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.WindowManager;

import com.magic.basic.utils.ScreenUtil;
import com.right.health.app.R;


/**
 * 输入型Dialog Base
 * <p>
 * Created by Hope on 16/4/5.
 */
public abstract class BaseDialog extends Dialog implements View.OnClickListener {

    protected View mRootView;

    protected IDialogDelegate mDelegate;

    private int mTag;

    public BaseDialog(Context context) {
        super(context, R.style.dialog);

        mRootView = getRootView(context);
        mRootView.setMinimumWidth((int) (ScreenUtil.getInstance(context).getScreenWidth() * 0.50));


        View cancelBtn = mRootView.findViewById(R.id.cancel_btn);
        if (cancelBtn != null) {
            cancelBtn.setOnClickListener(this);
        }

        View confirmBtn = mRootView.findViewById(R.id.yes_btn);
        if (confirmBtn != null) {
            confirmBtn.setOnClickListener(this);
        }

        setContentView(mRootView);
    }

    public BaseDialog setContent(String content) {
        return this;
    }

    public void setTitle(String title) {

    }


    public BaseDialog setDelegate(IDialogDelegate delegate) {
        this.mDelegate = delegate;
        return this;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel_btn:
                dismiss();
                break;
            case R.id.yes_btn:
                callback();
                break;
            default:
                break;
        }
    }

    @Override
    public void show() {
        super.show();
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = (int) (ScreenUtil.getInstance(getContext()).getScreenWidth() * 0.85f);
        getWindow().setAttributes(params);

    }

    private void  callback() {
        if (mDelegate != null) {
            mDelegate.onDialogCallback(this);
        }
    }

    public void setTag(int tag) {
        this.mTag = tag;
    }

    public int getTag() {
        return mTag;
    }

    protected abstract View getRootView(Context context);


    public interface IDialogDelegate {
         void onDialogCallback(BaseDialog dialog);
    }
}
